﻿$(function(){//延迟加载
	var gpId='';
	var functypeId='';
	var functypeId1='';//右键时记录
	var functypename1='';//右键时记录
	var funcrows=null;
	var functypename='';
	var functypesort='';
	var functypesort1='';
	var done=0;
	var newfuncrows=null;
	var deletefuncrows=null;
	$('#functypetree').tree({
		url:'getrootfunctype',
		onClick:function(node){
			functypeId=node.id;
			$('#ydpId').val(functypeId);
			$('#funclist').datagrid('reload',{
				functypeId:functypeId
			});
			functypename=node.text;
			functypesort=node.functypesort;
		},
		onContextMenu: function (e, node) {
			$('#tabsMenu').menu();
	        e.preventDefault();
	        functypeId=node.id;
	        functypeId1=node.id; 
	        functypename1=node.text;
	        functypesort1=node.functypesort;
	        $('#tabsMenu').menu('show', {
	            left: e.pageX,
	            top: e.pageY
	        });
	    },
	    onExpand:function(node){
			if (node){
	    		var cnode=$('#functypetree').tree('getChildren',node.target);
	    		if(cnode!=''){
		    		for(var i=0;i<cnode.length;i++){
		    			$('#functypetree').tree('remove',cnode[i].target);
		    		}		    			
	    		}
				$.ajax({
					url:'extendfunctypenode?nodeId='+node.id,
					success:function(data){
						$('#functypetree').tree('append', {
							parent: node.target,
							data:data
						});
					}
				});
			}
		}
	});
	$('#funclist').datagrid({
		url:'getfunclist',
		param:{
			functypeId:'',
		},
		columns:[[{
			field:'ck',
			checkbox:true
		},{
			field:'funcname',
			title:'功能名称',
			width:'10%',
			editor:{
	        	  type:'textbox',
	        		 options:{		        			 
	        			 required:true 
	        		 }
	          }
		},{
			field:'funcsort',
			title:"操作类型",
			width:'20%',
			editor:{
	        	  type:'textbox',
	        		 options:{		        			 
	        			 required:true 
	        		 }
	          }
		},{
			field:'functypename',
			title:"功能类型",
			width:'15%'
		},{
			field:'funcdescribe',
			title:"功能描述",
			width:'15%',
			editor:{
	        	  type:'textbox',
	        		 options:{		        			 
	        			 required:true 
	        		 }
	          }
		},{
			field:'funccode',
			title:"请求名称",
			width:'20%',
			editor:{
	        	  type:'textbox',
	        		 options:{		        			 
	        			 required:true
	        		 }
	          }
		}]],
		toolbar: [{
			iconCls: 'icon-add',
			text:'新增功能',
			handler: function(){
				append();
				}
		},'-',{
			iconCls: 'icon-save',
			text:'确定',
			handler: function(){
				accept();
			}
		},'-',{
			iconCls: 'icon-undo',
			text:'取消修改',
			handler: function(){
				reject();
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除功能',
			handler: function(){
				removeit();
			}
		}],
		fit:true,
		onDblClickRow:onClickRow,
		striped:true,
		nowrap:true,
		pagination:true,
		rownumbers:true,
		autoRowHeight:false
	});
	
	var pager = $('#funclist').datagrid('getPager');	// get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-ok',
			text:'保存修改',
			width:'150px',
			handler:function(){
				if(done==0){
					alert('请先确定修改');
					return;
				}
				alert(funcrows.length);
				var listupdate=JSON.stringify(funcrows);
				var listnew=JSON.stringify(newfuncrows);
				var listdelete=JSON.stringify(deletefuncrows);
				$.ajax({
					url:'updatefunclist',
					type:'POST',
					data:{
						listupdate:listupdate,
						listnew:listnew,
						listdelete:listdelete
					},
					success:function(data){
						alert(data.msg);
						$('#funclist').datagrid('reload',{
							functypeId:functypeId
						});
					}
				});
			}
		}]
	});
	
//	function addnewfunctype(){		
//		$('#addfunctype').dialog({
//			title:'添加新功能类别',
//			width:'330px',
//			height:'145px',
//			modal: true
//		});
//		$('#addfunctype').load('toaddnewfunctype',function(){
//			$.getScript('/xasj/js/func/addfunctype.js');
//		});
//	}
	$('#addnewfunctype').click(function(){
		if(functypesort1=='1'){
			alert('不能在页面功能添加子类别');
			return
		}
		$('#addfunctype').dialog({
			title:'添加新功能类别',
			width:'330px',
			height:'263px',
			modal: true
		});
		$('#addfunctype').load('toaddnewfunctype?parentId='+functypeId1+'&parentname='+functypename1,function(){
			$.getScript('/xasj/js/func/addfunctype.js');
		});
	});
	
	$("#deletefunctype").click(function(){
		$.ajax({
			url:'deletefunctype',
			type:'post',
			data:{
				functypeId:functypeId
			},
			success:function(data){
				alert(data.msg);
				$('#functypetree').tree('reload');
			}
		});
	});
	$('#toeditfunctype').click(function(){
		if(functypeId==''){
			return
		}
		$('#editfunctype').dialog({
			title:'编辑功能类别',
			width:'560px',
			height:'277px',
			modal:true
		});
		$('#editfunctype').load('toeditfunctype?functypeId='+functypeId,function(){
			$.getScript('/xasj/js/func/editfunctype.js');
		});
	});
	
	
	
	var editIndex = undefined;	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#funclist').datagrid('validateRow', editIndex)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		done=0;
		if (editIndex!= index){
			if (endEditing()){
				$('#funclist').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#funclist').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			if(functypesort!='1'){
				return
			}
			$('#funclist').datagrid('appendRow',{
				funcname: '',
				functypename:functypename,
				funcsort: '',
				funcdescribe:'',
				funccode:'',
				functypeId:functypeId
			});

			editIndex = $('#funclist').datagrid('getRows').length-1;
			$('#funclist').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		done=0;
		}
	
	function removeit(){
		if (editIndex == undefined){return}
		$('#funclist').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);

	}

	function accept(){
		if (endEditing()){
			done=1;
			funcrows = $('#funclist').datagrid('getChanges','updated');
			newfuncrows = $('#funclist').datagrid('getChanges','inserted');
			deletefuncrows=$('#funclist').datagrid('getChanges','deleted');
			$('#funclist').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#funclist').datagrid('rejectChanges');
		editIndex = undefined;
	}

	function getChanges(){
		var rows = $('#funclist').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
	
});